<template>
  <div>
    <!-- <p>文件下载</p> -->
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
export default {
  data() {
    return {}
  },
  computed: mapGetters(['netKing']),
  mounted() {
    this.$bus.$on('downloadFile', (v) => {
      if (!v) return
      this.downloadInit(v)
    })
    this.$bus.$on('downloadFileList', (list) => {
      list.forEach((v) => {
        this.downloadInit(v)
      })
    })
  },
  beforeDestroy() {
    this.$bus.$off('downloadFile')
    this.$bus.$off('downloadFileList')
  },
  methods: {
    async downloadInit(file) {
      let { id, resId, oldName, isDownload } = file
      if (this.netKing == 2 && isDownload == 2) {
        this.$message({
          message: '该文件不能下载',
          type: 'warning',
        })
        return
      }
      let res = await this.$store.dispatch('downloadFileTbNetDisk', resId || id)
      this.saveAs(oldName, res)
    },
    saveAs(name, data) {
      var urlObject = window.URL
      var export_blob = new Blob([data])
      var save_link = document.createElement('a')
      save_link.href = urlObject.createObjectURL(export_blob)
      save_link.download = name
      save_link.click()
    },
  },
}
</script>